package com.siyuanren.gcocp.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.util.UUIDUitl;
import com.siyuanren.gcocp.consts.GcocpConsts;
import com.siyuanren.gcocp.dto.wx.WxUserDTO;
import com.siyuanren.gcocp.entity.WxActive;
import com.siyuanren.gcocp.entity.WxUser;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.util
 * Date         2018/6/28
 * Time         13:37
 * Description
 */
public class QrCodeCreateUtil {

    /**
     * 生成活动二维码，分享图片地址
     * 返回生成二维码的相对路径
     */
    public static String genActiveQRcode(WxUser sponsorUser, Integer courseId, WxActive active, Integer voucherId) throws IOException {
        String shareUrl = String.format(PublicConfig.PUBLIC_WX_VOUCHER_URL, courseId, sponsorUser.getId(), active.getId(), voucherId);
        String qrcodeName = String.format("active_%s_%s_%s_%s_qrcode%s", sponsorUser.getId(), courseId, active.getId(), voucherId, GcocpConsts.QRCODE_SUFFIX);
        File qrcodeFile = FileUtil.file(PublicConfig.USER_DATA_PATH + File.separator + cn.hutool.core.date.DateUtil.formatDate(new Date()) + File.separator + qrcodeName);
        File dir = FileUtil.file(PublicConfig.USER_DATA_PATH + File.separator + cn.hutool.core.date.DateUtil.formatDate(new Date()));
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String authUrl = PublicConfig.TOMCAT_URL + "authAndGo?actualUrl=" + Base64.encode(shareUrl);
        QrCodeUtil.generate(authUrl, GcocpConsts.QRCODE_SIZE, GcocpConsts.QRCODE_SIZE, qrcodeFile);

        // 合成活动图片
        File userImage = FileUtil.file(PublicConfig.USER_DATA_PATH + sponsorUser.getUserImg());
        File activeBgFile = FileUtil.file(PublicConfig.RESOURCE_PATH + active.getBackgroundImg());

        String activeImg = String.format("active_%s_%s_%s_%s%s", sponsorUser.getId(), courseId, active.getId(), voucherId, GcocpConsts.QRCODE_SUFFIX);
        BufferedImage activeImage = NewImageUtil.getActiveImage(activeBgFile, qrcodeFile, userImage, sponsorUser.getNickname());
        NewImageUtil.generateFile(activeImage, PublicConfig.USER_DATA_PATH + cn.hutool.core.date.DateUtil.formatDate(new Date()) + File.separator + activeImg);

        qrcodeFile.delete();
        return DateUtil.formatDate(new Date()) + File.separator + activeImg;
    }

    /**
     * 生成活动邀请成功后的优惠券地址.
     * @param userId
     * @param courseId
     * @param active
     * @return
     * @throws IOException
     */
    public static String genActiveSponsorCourse(Integer userId, Integer courseId, WxActive active) throws IOException {
        // 优惠券,不在这里赠送. 生成一个没有优惠券的课程邀请码. 只用于进入课程.
        String voucher = genVoucherQRcode(userId, courseId, 0);
        // 合成活动图片
        File voucherFile = FileUtil.file(PublicConfig.USER_DATA_PATH + voucher);
        File activeBgFile = FileUtil.file(PublicConfig.RESOURCE_PATH + active.getSponsorVoucherImg());
        BufferedImage voucherImage = ImageIO.read(voucherFile);
        BufferedImage activeBgImage = ImageIO.read(activeBgFile);

        String activeImg = String.format("active_%s_%s_%s_%s%s", userId, courseId, active.getId(), active.getSponsorVoucherId(), GcocpConsts.QRCODE_SUFFIX);
        BufferedImage activeImage = NewImageUtil.watermark(activeBgImage, voucherImage, 123, 1170, 1);
        NewImageUtil.generateFile(activeImage, PublicConfig.USER_DATA_PATH + cn.hutool.core.date.DateUtil.formatDate(new Date()) + File.separator + activeImg);

        return DateUtil.formatDate(new Date()) + File.separator + activeImg;
    }


    /**
     * 生成课程个人专属二维码，分享图片地址
     * 返回生成二维码的相对路径
     */
    public static String genVoucherQRcode(Integer userId, Integer courseId, Integer voucherId) {
        if (voucherId == null) {
            voucherId = 0;
        }
        String qrcodeName = String.format("%s_%s_%s_%s", userId, courseId, voucherId, GcocpConsts.QRCODE_SUFFIX);
        File file = FileUtil.file(PublicConfig.USER_DATA_PATH + File.separator + DateUtil.formatDate(new Date()) + File.separator + qrcodeName);

        // 如果二维码不存在则重新生成
        if (!file.exists()) {
            String shareUrl = String.format(PublicConfig.PUBLIC_WX_VOUCHER_URL, courseId, userId, 0, voucherId);
            File dir = FileUtil.file(PublicConfig.USER_DATA_PATH + File.separator + DateUtil.formatDate(new Date()));
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String authUrl = PublicConfig.TOMCAT_URL + "authAndGo?actualUrl=" + Base64.encode(shareUrl);
            QrCodeUtil.generate(authUrl, GcocpConsts.QRCODE_SIZE, GcocpConsts.QRCODE_SIZE, file);
        }
        return DateUtil.formatDate(new Date()) + File.separator + qrcodeName;
    }

}
